package com.lu.stack.nacos.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lu.stack.common.model.domain.ApplicationContextDo;
import com.lu.stack.common.model.response.R;
import com.lu.stack.nacos.model.entity.Article;
import com.lu.stack.nacos.service.ArticleService;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhanglu
 * @project lu-cloud-stack
 * @description
 * @date 2023/1/30 17:01
 */
@RequestMapping("/test")
@RestController
public class TestController{

    @Resource
    private Environment env;
    @Resource
    private ArticleService articleService;

    @GetMapping("/t1")
    public Object t1(){
        return env.getProperty("application-message");
    }

    @GetMapping("/t2")
    public Object t2(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return articleService.list();
    }

    @GetMapping("/t5")
    public R t5(){
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return R.success();
    }

    @GetMapping("/t5s")
    public String t5s() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "ok";
    }

    @GetMapping("/t5ss")
    public ApplicationContextDo t5ss() {
        ApplicationContextDo applicationContextDo = new ApplicationContextDo();
        applicationContextDo.setApplication("test");
        return applicationContextDo;
    }

    @GetMapping("/t6")
    public R t6(@RequestParam String id){
        return R.success(id);
    }

    @GetMapping("/t7")
    public R t7(@RequestParam String id, @RequestParam String name){
        return R.success().params("id", id).params("name", name);
    }

    @PostMapping("/t8")
    public R t8(@RequestParam String id, @RequestParam String name, @RequestParam("file") MultipartFile multipartFile){
        return R.success().params("id", id).params("name", name).params("fileId", multipartFile.getOriginalFilename());
    }

    @PostMapping("/t{path}")
    public R t9(@PathVariable String path, @RequestParam String id, @RequestParam String name){
        return R.success().params("id", id).params("name", name).params("path", path);
    }

    @PostMapping("/t1{path}")
    public R t10(@PathVariable String path, @RequestParam String id, @RequestParam String name, @RequestBody Map<String, Object> json, HttpServletResponse response){
        if(path.equalsIgnoreCase("0")){
            return R.success().params("id", id).params("name", name).params("path", path).params("json", json);
        }else if(path.equalsIgnoreCase("1")){
            try {
                return R.success().params("id", id).params("name", name).params("path", path).params("json", json);
            } catch (Exception e) {
                return R.success(e.getMessage());
            }
        }
        return R.success();
    }

    @GetMapping("/t12")
    public void t12(@RequestParam String id, HttpServletResponse response) throws Exception {
        File file = new File(id);
        response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
        response.setHeader("Content-Type", "application/octet-stream");
        response.setContentType("application/octet-stream; charset=UTF-8");
        ServletOutputStream outputStream = response.getOutputStream();
        FileUtil.writeToStream(file, outputStream);
        outputStream.flush();
    }

    @PostMapping("/t13")
    public boolean t13(String data) throws Exception {
        return true;
    }

    @PostMapping("/t14")
    public Integer t14(String data) throws Exception {
        return 1;
    }

    @GetMapping("/t15")
    public int t15(String data) throws Exception {
        return 1;
    }

    @GetMapping("/t16")
    public byte[] t16(@RequestParam String id) throws Exception {
        return FileUtil.readBytes(id);
    }

    @GetMapping("/t17")
    public String t17(String id){
        return id;
    }

    @GetMapping("/t18")
    public List<String> t18(String id){
        return Arrays.asList("1", "2", "3");
    }

    @PostMapping("/t19")
    public List<Article> t19(String id){
        return articleService.list();
    }

    @GetMapping("/t20")
    public String[] t20(String id){
        return Arrays.asList("1", "2", "3").toArray(new String[3]);
    }

    @GetMapping("/t21")
    public Article[] t21(String id){
        List<Article> list = articleService.list();
        return list.toArray(new Article[list.size()]);
    }

    @GetMapping("/t22")
    public Object t22(){
        boolean save = articleService.save(new Article().setHeaderPid(-1L).setAuthor("zhanglu").setTitle("测试加解密").setUserId(-1L));
        Article article = articleService.getOne(new QueryWrapper<Article>().eq("user_id", -1L));
        return article;
    }
}
